/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.Oracle;

import Persistencia.Entidades.ResponsableSenasa;
import Persistencia.IResponsableSenasaDAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author zeta
 */
public class ResponsableSenasaOracleDAO implements IResponsableSenasaDAO {

    private static ResponsableSenasaOracleDAO INSTANCE = null;
    /*
     * El contrustcor private no permite que se genere un constructor por defecto
     */

    private ResponsableSenasaOracleDAO() {
    }

    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ResponsableSenasaOracleDAO();
        }
    }

    public static ResponsableSenasaOracleDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    public Integer insertResponsableSenasa(ResponsableSenasa resp) {
        String sql = "INSERT INTO RESPONSABLESENASA  (NOMBRE,APELLIDO)"
                + "VALUES(?,?) RETURNING ID_RESPONSABLE INTO ?";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setString(1, resp.getNombre());
            pst.setString(2, resp.getApellido());
            pst.registerReturnParameter(3, OracleTypes.NUMBER);
            pst.executeUpdate();

            ResultSet rs = pst.getReturnResultSet();
            if (rs.next()) {
                return rs.getInt(1);
            }
            return null;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public boolean deleteResponsableSenasa(ResponsableSenasa resp) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean updateResponsableSenasa(ResponsableSenasa resp) {
        String sql = "UPDATE RESPONSABLESENASA SET NOMBRE = ?,"
                + "APELLIDO = ? WHERE ID_RESPONSABLE = ?";

        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setString(1, resp.getNombre());
            pst.setString(2, resp.getApellido());
            pst.setInt(3, resp.getIdResponsable());

            return pst.execute();
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public ResponsableSenasa getResponsableSenasa(Integer idResponsable) {
        String sql = "SELECT * FROM RESPONSABLESENASA  WHERE ID_RESPONSABLE = ?";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setInt(1, idResponsable.intValue());
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                ResponsableSenasa resp = new ResponsableSenasa();
                resp.setIdResponsable(rs.getShort("ID_RESPONSABLE"));
                resp.setApellido(rs.getString("APELLIDO"));
                resp.setNombre(rs.getString("NOMBRE"));
                return resp;
            } else {
                return null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public ResponsableSenasa getResponsableSenasa(String nombreApellido) {
        String sql = "SELECT * FROM RESPONSABLESENASA  WHERE CONCAT(CONCAT(NOMBRE,' '),APELLIDO) LIKE '%?%' ";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            pst.setString(1, nombreApellido);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                ResponsableSenasa resp = new ResponsableSenasa();
                resp.setIdResponsable(rs.getShort("ID_RESPONSABLE"));
                resp.setApellido(rs.getString("APELLIDO"));
                resp.setNombre(rs.getString("NOMBRE"));
                return resp;
            } else {
                return null;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<ResponsableSenasa> getAllResponsablesSenasa() {
        String sql = "SELECT * FROM RESPONSABLESENASA";
        try {
            OraclePreparedStatement pst = (OraclePreparedStatement) OracleDAOFactory.getConnection().prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            List<ResponsableSenasa> lRespSenasa = new ArrayList();
            while (rs.next()) {
                ResponsableSenasa resp = new ResponsableSenasa();
                resp.setIdResponsable(rs.getShort("ID_RESPONSABLE"));
                resp.setApellido(rs.getString("APELLIDO"));
                resp.setNombre(rs.getString("NOMBRE"));
                lRespSenasa.add(resp);

            }
            return lRespSenasa;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<ResponsableSenasa> selectResponsablesSenasa(String condicion) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
